package cn.bone.gateway.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * Created by superbone on 2017/3/29.
 * spring线程池
 */
@Configuration
public class SpringThreadConfig {


    @Bean("loggerAsync")
    public Executor loggerAsync(){
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(20);   //核心线程数
        executor.setMaxPoolSize(200);   //最大线程数
        executor.setThreadNamePrefix("loggerAsync-");
        executor.initialize();
        return executor;
    }

    @Bean("businessAsync")
    public ThreadPoolTaskExecutor businessAsync(){
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(20);   //核心线程数
        executor.setMaxPoolSize(500);   //最大线程数
        executor.setThreadNamePrefix("businessAsync-");
        // rejection-policy：当pool已经达到max size的时候，如何处理新任务
        // CALLER_RUNS：不在新线程中执行任务，而是有调用者所在的线程来执行
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        executor.initialize();
        return executor;
    }
}
